package com.kaishengit.web;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.kaishengit.entity.Message;
import com.kaishengit.entity.UserProject;
import com.kaishengit.service.MessageService;
import com.kaishengit.util.StringUtil;

public class MessageServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method=request.getParameter("m");
		if(StringUtil.isEmpty(method)){
			list(request,response);
		}else if("save".equals(method)){
			save(request,response);
		}
}
	private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		UserProject userProject=(UserProject)session.getAttribute("userProject");
		
		String content=request.getParameter("content");
		MessageService service=new MessageService();
		service.save(content,userProject);
		response.sendRedirect("message.jspx");
	}
	
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		UserProject userProject=(UserProject)session.getAttribute("userProject");
		
		MessageService service=new MessageService();
		List<Message> messageList=service.findMessage(userProject);
		request.setAttribute("messageList", messageList);
		List<Message> atmessageList=service.findAtMessage(userProject);
		request.setAttribute("atmessageList", atmessageList);
		request.getRequestDispatcher("/WEB-INF/views/project.jsp").forward(request, response);
	}
}